package com.urerp.web.controller;

import java.util.HashMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.urerp.web.common.MenuUtil;
import com.urerp.web.common.SessionUtil;
import com.urerp.web.persistence.MemberMapper;

@Controller
public class LoginController {
	
	private static final Logger logger = LogManager.getLogger(LoginController.class); 
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private MenuUtil menuUtil;
	
	// 프로퍼티 파일 읽기
    @Value("#{code['member.pos']}") 			private String m_pos;
    @Value("#{code['member.status']}")			private String m_status;
    @Value("#{code['member.status.normal']}") 	private String m_status_normal;
    @Value("#{code['auth.level']}")				private String m_level;

    // 로그인 페이지 이동
	@RequestMapping(value = "login/loginForm", method = RequestMethod.GET)
    public String login() {
		
		logger.debug("RequestMapping : /login/loginForm, Method : login");
		
		if(SessionUtil.getSession()) {
			return "redirect:/index.ur";
		}
		
        return "login/login";
        
	}
	
	 // 아이디 사용 여부 체크
 	@RequestMapping(value = "login/loginMemberCheckStatus", method = RequestMethod.POST)
 	public @ResponseBody String loginMemberCheckStatus(@RequestParam HashMap<String, String> memberMap) {
 		
 		logger.debug("RequestMapping : /login/loginMemberCheckStatus, Method : loginMemberCheckStatus");
 		
 		if (memberMap != null) {
 			String status = memberMapper.getMemberStatus(memberMap);
 			
 			if (status != null) {
 				if (!status.equals(m_status_normal)) {
 	 				
 	 				// 정상 사용자가 아님
 	 				return "4";
 	 			}
 			} else {
 				return "9";
 			}
 			
 		} else {
 				
 			// 데이터 없음
 			return "9";
 		}
 		
 		return "0";
 	}
 	
	// 로그인 프로세스 처리 및 세션 저장
	@RequestMapping(value = "/login/submitLogin", method = RequestMethod.POST)
	public String submitLogin(Model model, @RequestParam HashMap<String, String> memberMap) {
		
		logger.debug("RequestMapping : /login/submitLogin, Method : submitLogin");
		
		memberMap.put("M_POS", 		m_pos);
		memberMap.put("M_STATUS", 	m_status);
		memberMap.put("M_LEVEL", 	m_level);
		
		memberMap = memberMapper.getMemberInfo(memberMap);
		
		if (memberMap == null) {
			return "redirect:/login/loginForm.ur";
		}
		
		// 세션 저장
		SessionUtil.setSession("sessionFlag", "Y");
		SessionUtil.setSession("memberSession", memberMap);
		menuUtil.setMenu();
		
		return "redirect:/index.ur";
	}
	
	// 로그 아웃
	@RequestMapping(value = "login/logout", method = RequestMethod.GET)
	public String logout() {
		
		logger.debug("RequestMapping : /login/logout, Method : logout");
		
		SessionUtil.invalidate();
		
		return "redirect:/login/loginForm.ur";
	}
	 
}
